package com.xltt.vehicleControl.activity;


import android.content.Intent;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xltt.vehicleControl.BuildConfig;
import com.xltt.vehicleControl.R;
import com.xltt.vehicleControl.base.BaseActivity;
import com.xltt.vehicleControl.base.MyApplication;
import com.xltt.vehicleControl.bean.ControlPushEvent;
import com.xltt.vehicleControl.contants.Constants;
import com.xltt.vehicleControl.fragment.VehicleFunctionFragment;
import com.xltt.vehicleControl.util.Logger;
import com.xltt.vehicleControl.util.SharedPreferencesUtils;
import com.xltt.vehicleControl.view.RingProgressView;

import butterknife.BindView;
import butterknife.OnClick;

public class PowerSettingsActivity extends BaseActivity {


    @BindView(R.id.power_btn)
    RingProgressView mPowerBtn;
    @BindView(R.id.time_layout)
    LinearLayout mTimeLayout;

    @BindView(R.id.pw_value_time_txt)
    TextView mTimeValue;
    @BindView(R.id.pw_add_time_btn)
    ImageView mPwAddTimeBtn;
    @BindView(R.id.pw_remove_time_btn)
    ImageView mPwRemoveTimeBtn;

    private static final String PW_TIME_KEY = "pw_time_key";

    @Override
    protected void initPresenter() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_power_settings;
    }

    @OnClick({R.id.pw_remove_time_btn,R.id.pw_add_time_btn})
    public void onViewClicked(View view) {
        String value = mTimeValue.getText().toString();
        if(view == mPwAddTimeBtn){
            int addValue = Integer.valueOf(value) + 1;
            if(addValue < 5){
                addValue = 5;
            }else if(addValue >= 5 && addValue < 8){
                addValue = 8;
            }else if(addValue >= 8 && addValue < 10){
                addValue = 10;
            }else{
                addValue = 10;
            }
            mTimeValue.setText(addValue + "");
        }else if(view == mPwRemoveTimeBtn){
            int removeValue = Integer.valueOf(value) - 1;
            if(removeValue < 5){
                removeValue = 3;
            }else if(removeValue >= 5 && removeValue < 8){
                removeValue = 5;
            }else if(removeValue >= 8 && removeValue < 10){
                removeValue = 8;
            }
            mTimeValue.setText(removeValue + "");
        }
        String saveTimeValue = mTimeValue.getText().toString();
        Logger.d("保存发动机启动时长 = " + saveTimeValue);
        SharedPreferencesUtils.setParam(this,PW_TIME_KEY,saveTimeValue);
    }

    @Override
    protected void initViews() {
        MyApplication application = ((MyApplication)getApplication());
        boolean state = application.isEngineStarted();
        mTimeLayout.setVisibility(state ? View.INVISIBLE : View.VISIBLE);
        Logger.d("MyApplication 当前车辆引擎状态 = " + state);
        mPowerBtn.setTag(state ? "stop" : "start");
        mPowerBtn.setIcon(state ? R.mipmap.stop_engine_bg : R.mipmap.start_engine_bg);
        mPowerBtn.setShowText(false);
        mPowerBtn.setRingWidthPercent(20);
        mPowerBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        start();
                        break;
                    case MotionEvent.ACTION_UP:
                        reset();
                        break;
                }
                return true;
            }
        });

        Object defaultTimeValue = SharedPreferencesUtils.getParam(this,PW_TIME_KEY,3 + "");
        mTimeValue.setText(defaultTimeValue + "");
    }

    private int progress;
    private static Handler mHandler = new Handler();
    private boolean isRunning;
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            mPowerBtn.setProgress(progress);
            if (progress < 30) {
                progress++;
                mHandler.postDelayed(this, 60);
            }else if(progress >= 30){
                powerClicked();
            } else {
                isRunning = false;
                mHandler.removeCallbacks(this);
            }
        }
    };
    private void start() {
        if (!isRunning) {
            mHandler.postDelayed(mRunnable, 30);
        }
        isRunning = true;
    }

    private void stop() {
        mHandler.removeCallbacks(mRunnable);
        isRunning = false;
    }

    private void reset() {
        stop();
        mHandler.postDelayed(mRunnableReset,1);
    }
    private Runnable mRunnableReset = new Runnable() {
        @Override
        public void run() {
            mPowerBtn.setProgress(progress);
            if (progress > 0) {
                progress--;
                mHandler.postDelayed(this, 1);
            } else {
                isRunning = false;
                mHandler.removeCallbacks(this);
            }
        }
    };

    private void powerClicked() {
        String state = mPowerBtn.getTag().toString();
        String saveTimeValue = mTimeValue.getText().toString();
        Intent intent = new Intent();
        intent.putExtra(VehicleFunctionFragment.COMMAND_PARAMETER,state);
        intent.putExtra(VehicleFunctionFragment.TIME_PARAMETER,saveTimeValue);
        setResult(VehicleFunctionFragment.POWER_SETTINGS_RESULT_CODE,intent);
        finish();
    }

}
